package com.tsing.bms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.entity.Order;
import com.tsing.bms.mapper.OrderMapper;
import com.tsing.bms.service.IOrderService;
import com.tsing.bms.vo.req.AddOrderReqVo;
import com.tsing.bms.vo.req.IdAndStatusReqVo;
import com.tsing.bms.vo.req.IdReqVo;
import com.tsing.bms.vo.req.OrderPageReqVo;
import com.tsing.bms.vo.resp.OrderCountRespVo;
import com.tsing.bms.vo.resp.OrderPageRespVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author TheTsing
 * @create 2022-05-04 14:08
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Override
    public Page<OrderPageRespVo> orderPage(OrderPageReqVo reqVo) {
        Page<Order> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
        List<OrderPageRespVo> list = baseMapper.orderPage(reqVo, page);
        Page<OrderPageRespVo> housePage = new Page<>();
        housePage.setTotal(page.getTotal());//总条数
        housePage.setCurrent(page.getCurrent());//页码
        housePage.setSize(page.getSize());//当前页个数
        housePage.setRecords(list);//当前页数据
        return housePage;
    }

    @Override
    public void updateOrder(IdAndStatusReqVo reqVo) {
        Order order = new Order();
        order.setId(reqVo.getId());
        order.setPay(reqVo.getStatus());
        baseMapper.updateById(order);
    }

    @Override
    public void addOrder(AddOrderReqVo reqVo) {
        Order order = new Order();
        BeanUtil.copyProperties(reqVo, order);
        baseMapper.insert(order);
    }

    @Override
    public OrderCountRespVo orderCount(IdReqVo reqVo) {
        return new OrderCountRespVo()
                .setCount1(baseMapper.orderCount(reqVo.getId(), 1))
                .setCount2(baseMapper.orderCount(reqVo.getId(), 2))
                .setCount3(baseMapper.orderCount(reqVo.getId(), 3));
    }

}
